package com.jiaquyun.jcps.base

import android.app.Application
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.View
import androidx.annotation.ColorRes
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.ToastUtils
import com.jiaquyun.jcps.R
import io.reactivex.disposables.CompositeDisposable
import org.greenrobot.eventbus.EventBus

abstract class BaseAbstractActivity : AppCompatActivity() {

    val dispose: CompositeDisposable = CompositeDisposable()

    /**
     * 需要绑定的视图ID
     */
    abstract val layoutResId: Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.setContentView(layoutResId)
        ActivityCollector.addActivity(this)

        this.initStatusBar()
        this.doBeforeInit()
        this.initView(savedInstanceState)
        this.initView()
        this.initData()
    }

    override fun onStop() {
        super.onStop()
        //隐藏软键盘
        if (KeyboardUtils.isSoftInputVisible((this@BaseAbstractActivity)))
            KeyboardUtils.hideSoftInput((this@BaseAbstractActivity))
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this)
    }

    /**
     * 初始化操作
     * */

    /**
     * 设置沉浸式状态栏
     * */
    private fun initStatusBar() {

        //设置状态栏模式
        BarUtils.setStatusBarLightMode(
            (this), stateBar().isLightMode
        )
        //设置状态栏颜色
        BarUtils.setStatusBarColor(
            (this), ColorUtils.getColor(stateBar().color)
        )
        //为标题栏添加状态栏的高度
        stateBar().view?.let {
            BarUtils.addMarginTopEqualStatusBarHeight(it)
        }
    }

    /**
     * 初始化视图和数据之前进行操作
     */
    open fun doBeforeInit() = Unit

    /**
     * 初始化视图
     */
    open fun initView() = Unit

    open fun initView(savedInstanceState: Bundle?) = Unit

    /**
     * 初始化数据
     */
    open fun initData() = Unit

    /**
     *
     * */
    open fun stateBar(): StatusBar = StatusBar()

    /**
     * toast提示
     * */
    fun toast(message: String) {
        ToastUtils.showShort(message)
    }

    /**
     * 创建加载布局
     * */
    fun createLoadingView(): View {
        return layoutInflater.inflate(R.layout.view_empty_load, null)
    }

    data class StatusBar(
        var isLightMode: Boolean = true,
        @ColorRes var color: Int = R.color.transparent,
        var view: View? = null
    )

    companion object{
        fun setCustomDensity(application: Application){
            val appDisplayMetrics: DisplayMetrics = application.resources.displayMetrics

        }
    }

}